package com.aluvfy.springdemo.controller;

import com.aluvfy.springdemo.pojo.ResponseMessage;
import com.aluvfy.springdemo.pojo.User;
import com.aluvfy.springdemo.pojo.UserDto;
import com.aluvfy.springdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController             //接口对象方法返回转换为json
@RequestMapping("/user")  //定义访问路径
public class UserController {

    @Autowired
    IUserService userService;

    //增加
    @PostMapping
//    @CrossOrigin(origins = "http://localhost:8083")
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user){
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);
    }

    //查询
    @GetMapping("/{userId}")
    public ResponseMessage<User> get(@PathVariable Integer userId){
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }

    //修改    @Validated 参数验证的注解  修改的时候要传入id，不然默认post
    @PutMapping
    public ResponseMessage<User> edit(@Validated @RequestBody UserDto user){
        User userNew = userService.edit(user);
        return ResponseMessage.success(userNew);
    }
    //删除
   @DeleteMapping("/{userId}")
    public ResponseMessage<User> delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();
    }

    //增删改查小项目：https://gitee.com/aluvfy/SpringDemo

}
